<template>
	<view>
		<u-sticky bgColor="#fff">
			<view class="search">
				<view class="search-item">
					<u-search placeholder="输入规范名称筛选"
						shape="square"
						v-model="searchValue"
						:showAction="false"
						@change="searchChange">
					</u-search>
				</view>
				<view class="add-button" @click="searchValue = ''">
					清空
				</view>
			</view>
		</u-sticky>
		<view class="">
			<u-empty
				v-if="list.length<1"
				marginTop="100"
				text="暂无数据"
				icon="https://www.zxsl.zxmap.cn/zxch-app/2023/07/21/005b6980c9514d1e8e4a0cee0de018e4.png">
			</u-empty>
			<u-swipe-action>
				<u-swipe-action-item
					:disabled="true"
					v-for="(item,index) in list" :key="item.id">
					<view class="swipe-action u-border-top u-border-bottom">
						<view class="PhotographerBoxR chgf-show">
							<view class="">
							<!-- <view class="" @click="gotoPage('/subPackTask/surveySpecification/prewPdf/index?url='+encodeURIComponent(item.specificationFileUrl))"> -->
								<view class="chgf-main-name"> 
									<view class="chgf-main-name_line"></view> 
									<view class="" style="margin-left: 5rpx;">{{item.name1}}</view> 
								</view>
								<view>{{item.name2}}</view>
							</view>
							<u-icon name="download" size="28" @click="dowloadFile(item)"></u-icon>
						</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</view>
		<!-- <u-sticky bgColor="#fff" class="bottom-chgf"> -->
			<view class="chgf-num">
				已收录规范{{total}}本
			</view> 
		<!-- </u-sticky> -->
	</view>
</template>

<script>
	import { getList } from '@/api/surveySpecification.js'
	export default {
		data() {
			return {
				searchValue:'',
				list:[],
				total:0
			}
		},
		onReachBottom(){
		},
		onPullDownRefresh(){
		},
		onShow() {
			this.searchValue = ''
			this.getList()
		},
		onLoad() {
		},
		methods: {
			async getList(search){
				const { data } = await getList(search);
				if(data.code == 200){
					this.list = JSON.parse(JSON.stringify(data.rows))
					this.total = data.total
					for(let i = 0; i < this.list.length; i++){
						this.list[i].name = this.list[i].specificationName.substring(0, this.list[i].specificationName.lastIndexOf("pdf")-1)
						this.list[i].name1 = this.list[i].name.substring(0, this.list[i].name.lastIndexOf("》")+1)
						this.list[i].name2 = this.list[i].name.substring(this.list[i].name.lastIndexOf("》")+1)
					}
				}else{
					uni.showToast({ title: '获取规范名称列表失败', icon: 'none' })
				}
			},
			searchChange(value){
				this.getList({specificationName:value})
			},
			dowloadFile(obj){
				var that = this
				// #ifdef APP-PLUS
				uni.showLoading({title: '正在下载……'});
				var url = obj.specificationFileUrl;   //文件名称可以在上传时进行保存，下载时取出，当文件名称中存在单双引号时，要做好处理，否则会报错
				var name = obj.specificationName;
				var dtask = plus.downloader.createDownload(url,{
					filename:"_downloads/"+name    //利用保存路径，实现下载文件的重命名
				},function(d,status){
					//d为下载的文件对象
					if(status==200){
						//下载成功,d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
						var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件已保存：' + fileSaveUrl, //保存路径
							duration: 3000,
						});
						plus.runtime.openFile(d.filename);	   //选择软件打开文件
					}else{	
						//下载失败
						plus.downloader.clear();        //清除下载任务
					}
				})
				dtask.start();//执行下载
				// #endif
				// #ifdef MP-WEIXIN
				//加载框动画
				uni.showLoading({title: '正在下载……'});
				uni.downloadFile({
					url: obj.specificationFileUrl,//下载地址接口返回
					success: (data) => {
						console.log('打印data',data)
						if (data.statusCode === 200) {
							//隐藏加载框
							uni.hideLoading();
							//文件保存到本地
							uni.saveFile({
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									// console.log('打印res',res)
									uni.showToast({
										icon: 'success',
										mask: true,
										// title: '文件已保存：' + res.savedFilePath, //保存路径
										title: '下载成功' , 
										duration: 2000,
									});
												
									//自动打开文档查看
									setTimeout(() => {
										var filePath = res.savedFilePath;
										uni.openDocument({  //新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
											filePath: filePath,
											showMenu: true,
											success: function (res) {
												 console.log('打开文档成功');
											}
										});
									}, 2000)
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件下载失败',
						});
					},
				});
				// #endif
			},
		}
	}
</script>

<style>
	page{
		background-color: #f6f6f6;
		color: #333333;
	}
	.add-button{
		line-height: 104rpx;
		color: #0081ff;
	}
	.chgf-show{
		width: 100%;
		display: flex;
		justify-content: space-between;
		font-size: 30rpx;
	}
	.chgf-main-name{
		display: flex;
		justify-content: flex-start;
		position: relative;
	}
	.chgf-main-name_line{
		position: absolute; /*绝对定位*/
		top: 0; /*Y轴方向偏移自身高度的50%*/
		left: 0; /*紧靠容器左边缘*/
		content: ''; /*伪元素需要有内容才能显示*/
		width: 16rpx; /*伪元素宽度*/
		height: 35rpx; /*伪元素高度*/
		background-color: #1890ff; /*伪元素颜色*/
	}
	.bottom-chgf{
		bottom: 0px;
		top: unset;
	}
	.chgf-num{
		position: sticky;
		z-index: 970;
		bottom: 0rpx;
		background-color: #fff;
		text-align: center;
		font-weight: 600;
	}
</style>